// pages/index/index.js
Page({
  data: {
    userInfo: null,
    hasUserInfo: false,
    statistics: {
      totalJobs: 0,
      activeJobs: 0,
      myApplications: 0,
      completedJobs: 0
    },
    hotJobs: [],
    notices: [],
    loading: true
  },

  onLoad() {
    this.checkLoginStatus();
    this.loadStatistics();
    this.loadHotJobs();
    this.loadNotices();
  },

  onShow() {
    this.checkLoginStatus();
  },

  // 检查登录状态
  checkLoginStatus() {
    const token = wx.getStorageSync('token');
    const userInfo = wx.getStorageSync('userInfo');
    
    if (token && userInfo) {
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      });
    } else {
      this.setData({
        userInfo: null,
        hasUserInfo: false
      });
    }
  },

  // 加载统计数据
  loadStatistics() {
    const token = wx.getStorageSync('token');
    if (!token) {
      this.setData({ loading: false });
      return;
    }

    wx.request({
      url: 'http://localhost:8080/api/user/statistics',
      method: 'GET',
      header: {
        'Authorization': `Bearer ${token}`
      },
      success: (res) => {
        if (res.data.success) {
          this.setData({
            statistics: res.data.data
          });
        }
      },
      fail: () => {
        // 模拟数据
        this.setData({
          statistics: {
            totalJobs: 156,
            activeJobs: 89,
            myApplications: 12,
            completedJobs: 8
          }
        });
      },
      complete: () => {
        this.setData({ loading: false });
      }
    });
  },

  // 加载热门岗位
  loadHotJobs() {
    wx.request({
      url: 'http://localhost:8080/api/jobs/hot',
      method: 'GET',
      success: (res) => {
        if (res.data.success) {
          this.setData({
            hotJobs: res.data.data
          });
        }
      },
      fail: () => {
        // 模拟数据
        this.setData({
          hotJobs: [
            {
              id: 1,
              title: '前端开发工程师',
              company: '某科技公司',
              salary: '8000-15000',
              location: '北京市朝阳区',
              type: '全职'
            },
            {
              id: 2,
              title: '服务员',
              company: '某餐饮公司',
              salary: '20-25/时',
              location: '上海市浦东新区',
              type: '兼职'
            },
            {
              id: 3,
              title: '快递员',
              company: '某物流公司',
              salary: '150-200/天',
              location: '广州市天河区',
              type: '全职'
            }
          ]
        });
      }
    });
  },

  // 加载公告
  loadNotices() {
    wx.request({
      url: 'http://localhost:8080/api/notices',
      method: 'GET',
      success: (res) => {
        if (res.data.success) {
          this.setData({
            notices: res.data.data
          });
        }
      },
      fail: () => {
        // 模拟数据
        this.setData({
          notices: [
            {
              id: 1,
              title: '平台升级公告',
              content: '为了给用户提供更好的服务体验，平台将于本周末进行系统升级...',
              time: '2024-01-10'
            },
            {
              id: 2,
              title: '春节放假通知',
              content: '根据国家法定节假日安排，平台春节期间的服务时间调整如下...',
              time: '2024-01-08'
            }
          ]
        });
      }
    });
  },

  // 跳转到登录页面
  goToLogin() {
    wx.navigateTo({
      url: '/pages/login/login'
    });
  },

  // 跳转到岗位详情
  goToJobDetail(e) {
    const jobId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/job-detail/job-detail?id=${jobId}`
    });
  },

  // 跳转到岗位列表
  goToJobs() {
    wx.switchTab({
      url: '/pages/jobs/jobs'
    });
  },

  // 跳转到我的申请
  goToApplications() {
    wx.switchTab({
      url: '/pages/applications/applications'
    });
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.loadStatistics();
    this.loadHotJobs();
    this.loadNotices();
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1000);
  }
});
